﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class ChooseColorSettings : View
    {
        private string header, colorPlayer1, colorPLayer2, colorBall, text1, text2, back;
        private SpriteFont headerFont, textFont;
        private Vector2 headerPos, colorPlayer1Pos, colorPLayer2Pos, colorBallPos, text1Pos, text2Pos, backPos;
        private Vector2 headerOrigin, colorPLayer1Origin, colorPLayer2Origin, colorBallOrigin, text1Origin, text2Origin, backOrigin;
        private int colorEntryPlayer1;
        private int colorEntryPlayer2, colorEntryBall;
        private int selectedEntry = 1;
        private int minEntry = 1;
        private int maxEntry = 4;
        private Color[] colors;
        private Game_objects.GameObject gameObjectBall;
        private Game_objects.GameObject gameObjectBat1;
        private Game_objects.GameObject gameObjectBat2;

        public ChooseColorSettings()
            : base()
        {
            header = "Choose colors";
            headerPos = new Vector2(Game.WindowWidth / 2, 35);

            colors = new Color[] { Color.White, Color.Red, Color.Blue, Color.Yellow, Color.Gold, Color.Green, Color.HotPink, Color.Violet, Color.Cyan }; //enum?
            gameObjectBall = GameObjectManager.GetGameObject(GameObjectIdentity.Ball);
            gameObjectBat1 = GameObjectManager.GetGameObject(GameObjectIdentity.BatLeft);
            gameObjectBat2 = GameObjectManager.GetGameObject(GameObjectIdentity.BatRight);
            colorEntryPlayer1 = 0;
            colorEntryPlayer2 = 0;
            colorEntryBall = 0;

            back = "Back";
            backPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 + 10);

            colorPlayer1 = "< Player 1 Color >";
            colorPlayer1Pos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 120);

            colorPLayer2 = "< Player 2 Color >";
            colorPLayer2Pos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 90);

            colorBall = "< Ball Color >";
            colorBallPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 150);

            text1 = "Use Right and Left keys to change colors";
            text1Pos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 50);

            text2 = "Press back when satisfied! ";
            text2Pos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 25);
        }

        public override void LoadContent(ContentManager content)
        {
            headerFont = content.Load<SpriteFont>("Text");
            textFont = content.Load<SpriteFont>("Text");
            headerOrigin = headerFont.MeasureString(header) / 2;
            colorPLayer1Origin = textFont.MeasureString(colorPlayer1) / 2;
            colorPLayer2Origin = textFont.MeasureString(colorPLayer2) / 2;
            colorBallOrigin = textFont.MeasureString(colorBall) / 2;
            text1Origin = textFont.MeasureString(text1) / 2;
            text2Origin = textFont.MeasureString(text2) / 2;
            backOrigin = textFont.MeasureString(back) / 2;
        }

        public override void Update(GameTime gameTime)
        {

            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.MenuDown)
            {
                if (selectedEntry + 1 > maxEntry)
                    selectedEntry = minEntry;
                else
                    selectedEntry++;
            }

            else if (Controls.MenuUp)
            {
                if (selectedEntry - 1 < minEntry)
                    selectedEntry = maxEntry;
                else
                    selectedEntry--;
            }
            switch (selectedEntry)
            {
                case 1: //color ball
                    if (Controls.MenuRight && colorEntryBall + 1 < colors.Length) colorEntryBall++;
                    if (Controls.MenuLeft && colorEntryBall > 0) colorEntryBall--;
                    break;
                case 2: //color player1
                    if (Controls.MenuRight && colorEntryPlayer1 + 1 < colors.Length) colorEntryPlayer1++;
                    if (Controls.MenuLeft && colorEntryPlayer1 > 0) colorEntryPlayer1--;
                    break;
                case 3: //color player 2
                    if (Controls.MenuRight && colorEntryPlayer2 + 1 < colors.Length) colorEntryPlayer2++;
                    if (Controls.MenuLeft && colorEntryPlayer2 > 0) colorEntryPlayer2--;
                    break;
                case 4:
                    if (Controls.Enter)
                    {
                        gameObjectBall.Color = colors[colorEntryBall];
                        gameObjectBat1.Color = colors[colorEntryPlayer1];
                        gameObjectBat2.Color = colors[colorEntryPlayer2];

                        ViewManager.State = GameState.Settings;
                        selectedEntry = 1;
                    }
                    break;
                default:
                    throw new ArgumentException("Invalid menu entry: " + selectedEntry);
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(headerFont, header, headerPos, Color.White, 0, headerOrigin, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(textFont, text1, text1Pos, Color.White, 0, text1Origin, 0.7f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(textFont, text2, text2Pos, Color.White, 0, text2Origin, 0.7f, SpriteEffects.None, 0.5f);

            if (selectedEntry == 1)
                spriteBatch.DrawString(textFont, "" + colorBall, colorBallPos, colors[colorEntryBall], 0, colorBallOrigin, 2.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 1)
                spriteBatch.DrawString(textFont, "" + colorBall, colorBallPos, colors[colorEntryBall], 0, colorBallOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry == 2)
                spriteBatch.DrawString(textFont, colorPlayer1, colorPlayer1Pos, colors[colorEntryPlayer1], 0, colorPLayer1Origin, 2.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 2)
                spriteBatch.DrawString(textFont, colorPlayer1, colorPlayer1Pos, colors[colorEntryPlayer1], 0, colorPLayer1Origin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry == 3)
                spriteBatch.DrawString(textFont, colorPLayer2, colorPLayer2Pos, colors[colorEntryPlayer2], 0, colorPLayer2Origin, 2.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 3)
                spriteBatch.DrawString(textFont, colorPLayer2, colorPLayer2Pos, colors[colorEntryPlayer2], 0, colorPLayer2Origin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry == 4)
                spriteBatch.DrawString(textFont, back, backPos, Color.Red, 0, backOrigin, 2.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 4)
                spriteBatch.DrawString(textFont, back, backPos, Color.White, 0, backOrigin, 1.0f, SpriteEffects.None, 0.5f);
        }
    }
}
